package com.example.backgroundservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.backgroundservice.entity.Student;
import com.example.backgroundservice.mapper.StudentMapper;
import com.example.backgroundservice.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cc
 * @since 2022-06-09
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Override
    public String add(String students) {
        List<Student> studentList = new ArrayList<>();
        List<JSONObject> list = JSONObject.parseObject(students, List.class);
        for (JSONObject jsonObject : list) {
            Student student = JSONObject.parseObject(JSONObject.toJSONString(jsonObject), Student.class);
            studentList.add(student);
        }
        saveBatch(studentList);
        return "后台新增数据成功";
    }

    @Override
    public String init() {
        remove(null);
        for (int i = 0; i < 10; i++) {
            Student student = new Student();
            student.setName("学生" + i);
            student.setAge(22);
            save(student);
        }
        return "初始化数据成功";
    }
}
